package cc.rengu.igas.momp.common.dao.impl;

import cc.rengu.igas.momp.common.dao.MompOrgInfoMapper;
import cc.rengu.igas.momp.common.entity.TBOrgInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 机构管理表数据库操作接口实现
 *
 * @author liujinan
 * @version 1.0.0
 * @date 2021/6/28 19:56
 */

public class MompOrgInfoMapperImpl implements MompOrgInfoMapper {

    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);

    /**
     * 使用代理商编号查询代理商信息数据库操作实现
     *
     * @return
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/7/20
     */
    @Override
    public List<TBOrgInfo> selectMompOrgInfoListBySuperOrgId(String instId, String superOrgId) throws Exception {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        rglog.error("DBPoolName=[{}]", dbPoolName);

        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMompOrgInfoListBySuperOrgId");

        // 按接口赋查询条件
        TBOrgInfo tbOrgInfo = new TBOrgInfo();
        tbOrgInfo.setOrgId(instId);
        tbOrgInfo.setSuperOrgId(superOrgId);

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, tbOrgInfo, TBOrgInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("查询法人机构<{}>的机构<{}>下的签约商户信息失败!", instId);
            return null;
        }

        return objectList.stream().map(item -> (TBOrgInfo) (item)).collect(Collectors.toList());
    }
}
